<template>
	<div>
		<el-breadcrumb separator-class="el-icon-arrow-right">
		  	<el-breadcrumb-item :to="{ path: '/goods' }">首页</el-breadcrumb-item>
		  	<el-breadcrumb-item>添加属性</el-breadcrumb-item>
		</el-breadcrumb>
		<el-form ref="form" :model="attr" label-width="80px">
		  	<el-form-item label="属性品类">
			    <el-select v-model="attr.cate_id" placeholder="选择品类">
			      	<el-option v-for="item in screenList" :key="item.id" :label="item.screen_cate_name" :value="item.id"></el-option>
			    </el-select>
			</el-form-item>

			<el-form-item label="可选属性">
			    <el-checkbox-group v-model="attrList">
				    <el-checkbox v-for="item in cateList" :key="item.id" :label="item.cate_name"></el-checkbox>
				</el-checkbox-group>
			</el-form-item>

		  	<!-- <el-form-item v-for="item in cateList" :key="item.id" :label="item.cate_name">
		    	<el-switch @change="selectCate(item.id)" v-model="item.isOpen"></el-switch>
		  	</el-form-item> -->

		  	<el-form-item>
		    	<el-button type="primary" @click="addAttr">添加属性</el-button>
		    	<el-button>取消</el-button>
		  	</el-form-item>
		</el-form>
		<el-dialog
		  	title="选择属性"
		  	:visible.sync="dialogVisible"
		  	width="30%"
		  	:before-close="handleClose">
		  	<el-transfer
		  		filterable
		  		:titles="['属性列表', '已赋属性']"
		    	:filter-method="filterMethod"
		    	filter-placeholder="请输入属性关键词"
		    	v-model="selectAttrList"
		    	:data="attrList">
		  	</el-transfer>
		  	<span slot="footer" class="dialog-footer">
		    	<el-button @click="dialogVisible = false">取 消</el-button>
		    	<el-button type="primary" @click="dialogVisible = false">确 定</el-button>
		  	</span>
		</el-dialog>
	</div>
</template>

<script type="text/javascript">
	export default{
		data(){
			return{
				token:"",
				dialogVisible:false,
				attr:{
					attr_name:"",
					status:true,
					cate_id:null
				},
				screenList:[],
				cateList:[],
				attrList:[],
				selectAttrList:[]
			}
		},
		created(){
			this.token = window.axios.defaults.headers.common['X-CSRF-TOKEN'];
			this.getScreenList();
			this.getCateList();
		},
		methods:{
			handleRemove(file, fileList) {
		        console.log(file, fileList);
		    },
		    handlePreview(file) {
		        console.log(file);
		   	},
		    handlePictureCardPreview(file) {
		        this.dialogImageUrl = file.url;
		        this.dialogVisible = true;
		    },
		    handleExceed(files, fileList) {
	        	this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
	      	},
		    beforeRemove(file, fileList) {
		        return this.$confirm(`确定移除 ${ file.name }？`);
		    },
		    addAttr(){
		    	axios.post('/api/attrs/add',{_token:this.token,attr_name:this.attr.attr_name,status:this.attr.status,cate_id:this.attr.cate_id})
		    		.then(result => {
		    			if(result.data.code == 200){
		    				this.$notify({
		    					type:"success",
		    					title:"成功添加",
		    					message:result.data.message
		    				})
		    				this.attr.attr_name = ""
		    			}else{
		    				this.$notify.error(result.data.message);
		    			}
		    		})
		    		.catch(error => {

		    		})
		    },
		    getScreenList()
		    {
		    	axios.get('/api/screens')
					.then(result => {
						console.log(result);
						if(result.data.code == 200){
							this.screenList = result.data.result;
						}
					})
					.catch(error => {

					})
		    },
		    getCateList()
		    {
		    	axios.get('/api/cates')
					.then(result => {
						console.log(result);
						if(result.data.code == 200){
							for(var i=0;i<result.data.result.length;i++){
								result.data.result[i]['isOpen'] = false; 
							}
							this.cateList = result.data.result;
						}
					})
					.catch(error => {

					})
		    },
		    selectCate(id){
		    	axios.get('/api/attrs?id='+id)
		    		.then(result => {
		    			if(result.data.code == 200){
		    				this.attrList = this.handleAttrData(result.data.result);
		    				this.dialogVisible = true;
		    			}
		    		})
		    		.catch(error => {

		    		})
		    },
		    handleAttrData(data){
				var temp = [];
				for(var i=0;i<data.length;i++){
					temp.push({label:data[i].attr_name, key:'{"attr_id":'+data[i].id+',"cate_id":'+data[i].cate_id+'}'});
				}
				return temp
			},
			filterMethod(query,item){
				return item;
			},
			handleClose(){
				this.dialogVisible = false;
			}
		}
	}
</script>

<style type="text/css">
	input[type=file] {
	    display: none;
	}
</style>